package com.open.yujiakj.core.utils.springUtils;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.context.WebApplicationContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
@Slf4j
public class BeanUtils4Web
{
	private WebApplicationContext wac = null;


	/**
	 * 返回集合.
	 *
	 * @param beanName
	 * @param methodName
	 * @return
	 */
	public Collection list(String beanName, String methodName) throws Exception
	{
		try
		{
			Object object = get(beanName, methodName);

			return object == null ? new java.util.ArrayList() : (Collection) object;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 返回集合.
	 *
	 * @param beanName
	 * @param methodName
	 * @return
	 */
	public Object invoke(String beanName, String methodName) throws Exception
	{
		Object object = get(beanName, methodName);
		return object;
	}

	/**
	 * 返回指定对象.
	 *
	 * @param beanName
	 * @param methodName
	 * @return
	 * @throws Exception
	 */
	public Object get(String beanName, String methodName) throws Exception
	{
		Object _value = null;
		if (wac == null)
		{
			log.error("WebApplicationContext is not init.");
		} else
		{
			Object _bean = wac.getBean(beanName);
			if (_bean == null)
				return null;

			if (methodName.indexOf("(") == -1)
				_value = PropertyUtils.getProperty(_bean, methodName);
			else
				_value = getObjByStringMethod(_bean, methodName);

		}
		return _value;
	}

	/**
	 * 获得一个bean中的一个方法
	 *
	 * @param key
	 * @param column
	 * @param value
	 * @return
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private Object getObjByStringMethod(Object obj, String method) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, Exception
	{
		Object object = null;
		if (obj != null && method.indexOf("(") != -1)
		{
			String methodName = method.substring(0, method.indexOf("("));
			Object[] params = method.substring(method.indexOf("(") + 1,
					method.indexOf(")")).split(",");
			Method met = null;
			if (params.length > 0 && String.valueOf(params[0]).length() != 0)
			{
				//获取参数类型
				Class[] paramTypes = null;
				for (Method m : obj.getClass().getMethods())
				{
					paramTypes = m.getParameterTypes();
					if (paramTypes.length == params.length && methodName.equals(m.getName()))
					{
						met = m;
						break;
					}
				}
				if (met != null)
				{
					Object[] realParams = new Object[params.length];
					for (int _i = 0; _i < params.length; _i ++)
					{
						if (String.valueOf(paramTypes[_i]).contains("String"))
						{
							realParams[_i] = params[_i];
						}
						else
						{
							//常规数据类型都有valueOf，获取这个方法
							Method valueOfMethod = paramTypes[_i].getMethod("valueOf",
									String.class);
							//构造参数类型
							realParams[_i] = valueOfMethod.invoke(paramTypes[_i], params[_i]);
						}
					}
					object = met.invoke(obj, realParams);
				}
				else
				{
					log.error("不存在这个方法，请检查");
					throw new Exception("");
				}
			} else
			{
				met = obj.getClass().getMethod(methodName, null);
				object = met.invoke(obj, null);
			}
		}
		return object;
	}

	/**
	 * 初始化工具类. 获得spring上下文
	 *
	 * @param sc
	 * @throws Exception
	 */
	public void initUtils(WebApplicationContext wac) throws Exception
	{
		this.wac = wac;
		log.debug("初始化webUtils工具");
	}
}